<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Http\Requests\ApplicationAuthRequest;
use App\Http\Responses\CustomResponse;
use App\Services\admin\ApplicationAuthService;
use Illuminate\Http\Request;

/**
 * 应用权限管理
 * User: zhx
 * Date: 2023/10/27 0027
 * Time: 14:48
 */
class ApplicationAuthController extends BaseController
{
    /**
     * 列表
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function index(Request $request)
    {
        $list = ApplicationAuthService::getPageData($request->all());

        return CustomResponse::success($list);
    }

    /**
     * 获取顶级菜单列表
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function getApplicationAuth(Request $request)
    {
        $list = ApplicationAuthService::getData($request->all());

        $type = $request->input('ctype', '1');
        // 列表数据
        if ($type == 1) {
            $returnArray = [];

            // 递归函数，用于构建菜单层次结构
            function buildMenu($items, $parent_id = 0, $prefix = '')
            {
                $result = [];
                $totalItems = count($items);
                foreach ($items as $index => $item) {
                    $label = ($index === $totalItems - 1) ? $prefix.'└ '.$item->name : $prefix.'├ '.$item->name;
                    if ($item->parent_id == $parent_id) {
                        $result[] = [
                            'label' => $label,
                            'value' => $item->id,
                        ];
                        // 递归调用，为下级菜单项添加适当的前缀
                        $children = buildMenu($items, $item->id, $prefix.'│ ');
                        $result = array_merge($result, $children);
                    }
                }

                return $result;
            }

            $returnArray = buildMenu($list);

            array_unshift($returnArray, ['label' => '顶级', 'value' => 0]);

            return CustomResponse::success($returnArray);
        } elseif ($type == 2) {
            // 表单多选
            $returnArray = [];
            // 递归函数，用于构建菜单层次结构
            function buildMenu($items, $parent_id = 0)
            {
                $result = [];
                foreach ($items as $item) {
                    if ($item->parent_id == $parent_id) {
                        $children = buildMenu($items, $item->id);

                        $menu = [
                            'value' => $item->id,
                            'label' => $item->name,
                        ];

                        if (! empty($children)) {
                            $menu['children'] = $children;
                        }
                        $result[] = $menu;
                    }
                }

                return $result;
            }

            $returnArray = buildMenu($list);

            return CustomResponse::success($returnArray);
        }
    }

    /**
     * 创建/编辑
     *
     * @param  Request  $request
     * @return void
     */
    public function store(ApplicationAuthRequest $request)
    {
        if ($request->input('id')) {
            // 编辑
            $requestAll = $request->all(['id', 'app_id', 'parent_id', 'name', 'type', 'http_path', 'icon', 'order']);

            $id = ApplicationAuthService::edit($requestAll['id'], $requestAll);
            if ($id) {
                return CustomResponse::success($id, '编辑成功');
            } else {
                return CustomResponse::error('编辑失败');
            }
        } else {
            // 创建
            $requestAll = $request->all(['app_id', 'parent_id', 'name', 'type', 'http_path', 'icon', 'order']);
            $result = ApplicationAuthService::create($requestAll);
            if ($result) {
                return CustomResponse::success($result->id, '创建成功');
            } else {
                return CustomResponse::error('创建失败');
            }
        }
    }

    /**
     * 删除
     *
     * @return void
     */
    public function destroy(Request $request, $id)
    {
        $id = ApplicationAuthService::destroy(explode(',', $id));
        if ($id) {
            return CustomResponse::success($id, '删除成功');
        } else {
            return CustomResponse::error('删除失败');
        }
    }
}
